package de.hoits.android;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import de.hoits.android.database.TodoDbAdapter;

public class TodosOverview extends ListActivity
{
	private TodoDbAdapter dbHelper;
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
	private static final int DELETE_ID = Menu.FIRST + 1;
	private Cursor cursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todo_list);
		this.getListView().setDividerHeight(2);
		dbHelper = new TodoDbAdapter(this);
		dbHelper.open();
		fillData();
		registerForContextMenu(getListView());
	}

	// Create the menu based on the XML defintion
	@Override
	public boolean onCreateOptionsMenu(final Menu menu)
	{
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listmenu, menu);
		return true;
	}

	// Reaction to the menu selection
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.insert:
			createTodo();
			break;
		case R.id.about:
			showabout();
			break;
		default:
			return super.onMenuItemSelected(featureId, item);
		}

		return true;
	}

	private void showabout()
	{
		final Context mContext = this; // bug in der Doku :(
		final Dialog dialog = new Dialog(mContext);

		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle("Custom Dialog");

		final TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("Hello, this is a custom dialog!");
		final ImageView image = (ImageView) dialog.findViewById(R.id.image);
		image.setImageResource(R.drawable.reminder);

		dialog.show();
		// dialog.hashCode();
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.insert:
			createTodo();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(final MenuItem item)
	{
		switch (item.getItemId())
		{
		case DELETE_ID:
			final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			dbHelper.deleteTodo(info.id);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	private void createTodo()
	{
		final Intent i = new Intent(this, TodoDetails.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	// ListView and view (row) on which was clicked, position and
	@Override
	protected void onListItemClick(final ListView l, final View v, final int position, final long id)
	{
		super.onListItemClick(l, v, position, id);
		final Intent i = new Intent(this, TodoDetails.class);
		i.putExtra(TodoDbAdapter.KEY_ROWID, id);
		// Activity returns an result if called with startActivityForResult

		startActivityForResult(i, ACTIVITY_EDIT);
	}

	// Called with the result of the other activity
	// requestCode was the origin request code send to the activity
	// resultCode is the return code, 0 is everything is ok
	// intend can be use to get some data from the caller
	@Override
	protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
	{
		super.onActivityResult(requestCode, resultCode, intent);
		fillData();
	}

	private void fillData()
	{
		cursor = dbHelper.fetchAllTodos();
		startManagingCursor(cursor);

		final String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };
		final int[] to = new int[] { R.id.label };

		// Now create an array adapter and set it to display using our row
		final SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.todo_row, cursor,
				from, to);
		setListAdapter(notes);
	}

	@Override
	public void onCreateContextMenu(final ContextMenu menu, final View v,
			final ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		if (dbHelper != null)
		{
			dbHelper.close();
		}
	}
}
